#include "svdpi.h"
#include "common.h"
#include <vector>
#include <string>
class BusyTable
{
public:
    int size;
    std::vector<bool> table;
    BusyTable(int size) { resize(size); }
    void reset()
    {
        for (size_t i = 0; i < size; i++)
        {
            table[i] = true;
        }
    }
    void resize(int size)
    {
        table.resize(size);
        this->size = size;
        reset();
    }
    bool read(int prf_idx)
    {
        Assert(prf_idx < size, "PRF_IDX out of bound");
        return table[prf_idx];
    }
    void set(int prf_idx)
    {
        Assert(prf_idx < size, "PRF_IDX out of bound");
        table[prf_idx] = true;
    }
    void clear(int prf_idx)
    {
        Assert(prf_idx < size, "PRF_IDX out of bound");
        table[prf_idx] = false;
    }
};

static std::vector<BusyTable *> busytables;
static std::vector<std::string> busytable_names;
static int busytable_id = 0;
#ifdef __cplusplus
extern "C"
{
#endif
    void busytable_clear(int id, int prf_idx)
    {
        if (id == 0)
            return;
        auto bt = busytables[id - 1];
        bt->clear(prf_idx);
    }
    int busytable_create(int id, int prf_size, const char *name)
    {
        if (id == 0)
        {
            busytables.push_back(new BusyTable(prf_size));
            busytable_names.push_back(std::string(name));
            busytable_id++;
            return busytable_id;
        }
        else
        {
            auto bt = busytables[id - 1];
            bt->reset();
            return id;
        }
    }
    void busytable_read(int id, int prf_idx, svBit prf_valid)
    {
        if (id == 0)
            return;
        auto bt = busytables[id - 1];
        auto ref_prf_valid = bt->read(prf_idx);
        Assert(prf_valid == ref_prf_valid, "BusyTable read prf_idx %d result not equal REF[%d] DUT[%d]", prf_idx, ref_prf_valid, prf_valid);
    }
    void busytable_set(int id, int prf_idx)
    {

        if (id == 0)
            return;
        auto bt = busytables[id - 1];
        bt->set(prf_idx);
    }
#ifdef __cplusplus
}
#endif